//#include "_target.h"
//#include "mcp2515.h"

#include <FU68xx_2.h>
#include <Myproject.h>

//CanMsg_t msg_t;

//#define SPI1_CS0            GPIO_OUT(GPIOB_BASE, 0)
//#define SPI1_CS1            GPIO_OUT(GPIOB_BASE, 1)

//static void Spi_Enable(int8u ch)
//{
//    int8u i;
//
//    if (ch == 0) SPI1_CS0 = 0;
//    else if (ch == 1) SPI1_CS1 = 0;
//    for (i = 0; i < 10; i++);
//}
//
//static void Spi_Disable(void)
//{
//    int8u i;
//    
//    SPI1_CS0 = 1;
//    SPI1_CS1 = 1;
//    for (i = 0; i < 10; i++);
//}
//
//static int8u Spi_Putch(int8u data)
//{
//    int16u timeOut;
//    
//    timeOut = 800;
//    while (!(SPI1->SR & SPI_I2S_FLAG_TXE) && timeOut) timeOut--;
//    SPI1->DR = data;
//    timeOut = 800;
//    while (!(SPI1->SR & SPI_I2S_FLAG_RXNE) && timeOut) timeOut--;
//
//    return (SPI1->DR);
//}

//static void InitSpi(void)
//{
//    SPI_InitTypeDef  SPI_InitStructure;
//
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
//    Spi_Disable();
//
//    /* spi configuration -----------------------------------------------------*/
//    SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
//    SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
//    SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
//    SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;
//    SPI_InitStructure.SPI_CPHA              = SPI_CPHA_1Edge;
//    SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
//    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
//    SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
//    SPI_InitStructure.SPI_CRCPolynomial     = 7;
//    SPI_Init(SPI1, &SPI_InitStructure);
//    
//    /* enable spi ------------------------------------------------------------*/
//    SPI_Cmd(SPI1, ENABLE);
//}

void Mcp2515_Read(uint8 addr, uint8 *pBuf, uint8 len)
{
    uint8 i;

    Spi_Enable();
//	delay_ms(delay_time_1);
	
    Spi_Putch(READ);
    Spi_Putch(addr);
    
    for (i = 0; i < len; i++)
    {
          *pBuf = Spi_Putch(0);
		  pBuf ++;

		//  Spi_Getch(pBuf ++ );
		
       //  	delay_ms(delay_time_);		
		//	*pBuf  =	SPI_DR;
		//      pBuf++;

    }

//	delay_ms(delay_time_1);
    Spi_Disable();
}

void Mcp2515_ReadByte(uint8 addr, uint8 *pBuf)
{
    Spi_Enable();
//	delay_ms(delay_time_1);
	
    Spi_Putch(READ);
    Spi_Putch(addr);
    
    *pBuf = Spi_Putch(0);
	//Spi_Getch(pBuf );

	//delay_ms(delay_time_);
	// *pBuf = SPI_DR;;

//	delay_ms(delay_time_1);
    Spi_Disable();
}

void Mcp2515_Write(uint8 addr, uint8 *pBuf, uint8 len)
{
    uint8 i;

    Spi_Enable();
//	delay_ms(delay_time_1);
	
    Spi_Putch(WRITE);
    Spi_Putch(addr);
    
    for (i = 0; i < len; i++)
    {
        Spi_Putch(*pBuf);
        pBuf++;
    }

	//delay_ms(delay_time_1);
    Spi_Disable();
}

void Mcp2515_WriteByte(uint8 addr, uint8 pBuf)
{
    Spi_Enable();
//	delay_ms(delay_time_1);
	
    Spi_Putch(WRITE);
    Spi_Putch(addr);
    Spi_Putch(pBuf);

//	delay_ms(delay_time_1);
    Spi_Disable();
}

void Mcp2515_WriteBits(uint8 addr, uint8 pda, uint8 mask)
{
    Spi_Enable();
//	delay_ms(delay_time_1);
    
    Spi_Putch(BIT_MOD);
    Spi_Putch(addr);
    Spi_Putch(mask);
    Spi_Putch(pda);

//	delay_ms(delay_time_1);    
    Spi_Disable();
}

uint8 Mcp2515_Status()
{
    uint8 pda;

    Spi_Enable();
//	delay_ms(delay_time_1);
	
    Spi_Putch(RD_STAT);

	pda = Spi_Putch(0);
	//Spi_Getch(&pda );
	
    //delay_ms(delay_time_);
    // pda = SPI_DR;
  

//	delay_ms(delay_time_1);
    Spi_Disable();
	
    return pda;
}

void Mcp2515_Rts()
{
    Spi_Enable();
//	delay_ms(delay_time_1);
	
    Spi_Putch(RTS | TX0RTS);

//	delay_ms(delay_time_1);
    Spi_Disable();
}

void Mcp2515_Trans(uint8 *pMsg, uint8 len)
{

    Mcp2515_Write(TXB0SIDH, pMsg, len);                 //  wirte to mcp2515 registor
    Mcp2515_Rts();
}

void Mcp2515_Send(uint8 *pMsg, uint8 len)
{
	Mcp2515_Write(TXB0SIDH, pMsg, len);
	Mcp2515_WriteByte(TXB0CTRL, TXB_TXREQ_M );
	//p2515_Req();
}

bool Mcp2515_Recv(uint8       fifo, uint8 *pMsg, uint8 len)
{
     
    if (fifo) 
		{ Mcp2515_Read(RXB1SIDH, pMsg, len); }
    else 
		{ Mcp2515_Read(RXB0SIDH, pMsg, len); }
    
   
    return true;
}

void Mcp2515_soft_reset(void)
{
	Spi_Disable();
	delay_ms(4);

    Spi_Enable();
	delay_ms(2);
	
    Spi_Putch(RESET);

	delay_ms(2);
    Spi_Disable();
}

void Init_Mcp2515()
{
    uint16 i;
    uint8  pda;
	  uint8 timer;
    uint8  buf[12] = {0};
    
 //   InitSpi();
    
//    Spi_Enable(ch);
//    Spi_Putch(RESET);
//    Spi_Disable();
    pda=0;
    Mcp2515_soft_reset();
    delay_ms(100);
    
    Mcp2515_WriteByte(CANCTRL, MODE_CONFIG);
    Mcp2515_ReadByte(CANSTAT, &pda);
	if(pda != MODE_CONFIG){
		Mcp2515_WriteByte(CANCTRL, MODE_CONFIG);
	}

    Mcp2515_WriteByte(TXB0CTRL, 0x00);// priority level 0 < 1 < 2 < 3  
    Mcp2515_WriteByte(TXB1CTRL, 0x00);
    Mcp2515_WriteByte(TXB2CTRL, 0x00);

//    Mcp2515_WriteByte(CNF1, SJW1 + BRP10);
//    pda = BTLMODE_CNF3 + SAMP3 + SEG6 * 8 + SEG2;
//    Mcp2515_WriteByte(CNF2, pda);
//    Mcp2515_WriteByte(CNF3, SEG7);

     Mcp2515_WriteByte(CNF1, 0x03);//125000
	Mcp2515_WriteByte(CNF2,0x80|0x10|0 /*0x90*/);
	Mcp2515_WriteByte(CNF3, 0x02);


//	MCP2515_WriteByte(CANINTF,0x00);//清空CAN中断标志寄存器的所有位(必须由MCU清空)
//	MCP2515_WriteByte(CANINTE,0x01);//配置CAN中断使能寄存器的接收缓冲器0满中断使能,其它位禁止中断

//	Mcp2515_WriteByte(CANINTE, 0x00);
    Mcp2515_WriteByte(CANINTF,0x00);//清空CAN中断标志寄存器的所有位(必须由MCU清空)
  //  Mcp2515_WriteByte(CANINTE,  RX0INT);

    Mcp2515_WriteByte(CANINTE, RX1INT | RX0INT/*|IVRINT|ERRINT*/);


    Mcp2515_WriteByte(TXB0SIDH, 0x00);
    Mcp2515_WriteByte(TXRTSCTRL, 0x00);

//	Mcp2515_WriteByte(TXB0SIDH,0xFF);//发送缓冲器0标准标识符高位
//	Mcp2515_WriteByte(TXB0SIDL,0x07);//发送缓冲器0标准标识符低位


//	Mcp2515_WriteByte(RXB0SIDH,0x00);//清空接收缓冲器0的标准标识符高位
//	Mcp2515_WriteByte(RXB0SIDL,0x00);//清空接收缓冲器0的标准标识符低位

//	Mcp2515_WriteByte( RXB0CTRL,/* 0x20*/ 0x60 );//receive all the message

    Mcp2515_Write( RXF0SIDH, buf, 12);
    Mcp2515_Write( RXF3SIDH, buf, 12);
    Mcp2515_Write( RXM0SIDH, buf, 8);

    // added 
	Mcp2515_WriteByte(RXM0SIDH,0xFF);//配置验收屏蔽寄存器n标准标识符高位
	Mcp2515_WriteByte(RXM0SIDL,0xE0);//配置验收屏蔽寄存器n标准标识符低位
	Mcp2515_WriteByte(RXF0SIDH,0x00);//配置验收滤波寄存器n标准标识符高位
	Mcp2515_WriteByte(RXF0SIDL,0x00);//配置验收滤波寄存器n标准标识符低位
	Mcp2515_WriteByte(RXF1SIDH,0x00);//配置验收滤波寄存器n标准标识符高位
	Mcp2515_WriteByte(RXF1SIDL,0x00);//配置验收滤波寄存器n标准标识符低位	
	
	Mcp2515_WriteByte(RXM1SIDH,0xFF);//配置验收屏蔽寄存器n标准标识符高位
	Mcp2515_WriteByte(RXM1SIDL,0xE0);//配置验收屏蔽寄存器n标准标识符低位
	Mcp2515_WriteByte(RXF2SIDH,0x00);//配置验收滤波寄存器n标准标识符高位
	Mcp2515_WriteByte(RXF2SIDL,0x00);//配置验收滤波寄存器n标准标识符低位	
//	Mcp2515_WriteByte(RXF3SIDH,0x00);//配置验收滤波寄存器n标准标识符高位
//	Mcp2515_WriteByte(RXF3SIDL,0x00);//配置验收滤波寄存器n标准标识符低位	
//	Mcp2515_WriteByte(RXF4SIDH,0x00);//配置验收滤波寄存器n标准标识符高位
//	Mcp2515_WriteByte(RXF4SIDL,0x00);//配置验收滤波寄存器n标准标识符低位	
//	Mcp2515_WriteByte(RXF5SIDH,0x00);//配置验收滤波寄存器n标准标识符高位
//	Mcp2515_WriteByte(RXF5SIDL,0x00);//配置验收滤波寄存器n标准标识符低位	
	
	Mcp2515_WriteByte( RXB0CTRL, 0x24/*0x60*/ );
	Mcp2515_WriteByte( RXB1CTRL, 0x20/*0x60*/ );


    Mcp2515_WriteByte( CANCTRL, MODE_NORMAL | CLKEN /*|OSM_ENABLED*/);
	//Mcp2515_WriteByte(CANCTRL, MODE_CONFIG);

//	Mcp2515_WriteByte( CANCTRL, MODE_LOOPBACK | CLKEN /*| CLK8*/);


    for (i = 0; i < 4000; i++);
    Mcp2515_ReadByte(CANSTAT, &pda);
    timer = 0;
    while (pda & 0xe0)
    {
        Mcp2515_WriteByte(CANCTRL, MODE_CONFIG);
        Mcp2515_WriteByte(CANCTRL, MODE_NORMAL | CLKEN /*|OSM_ENABLED*/);
        for (i = 0; i < 4000; i++);
	     	Mcp2515_ReadByte(CANSTAT, &pda);
        timer++;
        if (timer > 100) return;
    }

//	Mcp2515_WriteByte( RXB0CTRL, 0x60);//receive all the message

   // Mcp2515_WriteByte( RXB0CTRL, 0x64);
   // Mcp2515_WriteByte( RXB1CTRL, 0x00);

    Mcp2515_WriteByte( CANINTF,  0x00);  
	 delay_ms(10000);		
	// can_status =can_ok;
	 

}

